(function() {

	angular.module('cf-content').directive('auth', ['$timeout', "$window", function($timeout, $window,EVENT) {
		return {
			restrict: 'AE',
			templateUrl:'app/page/authmanage/authmanage.html',
			// replace:true,
			controller:authmanage,
			controllerAs:'authCtrl'
		}
	}])

	authmanage.$inject=['$scope','Api','$uibModal','$q']

	function authmanage($scope,Api,$uibModal,$q){
		var vm = this;
        vm.toggleCheckbox=false;
        vm.status='checking';
        //pagination
    		vm.pagination={
    			max_size:5,
    			page_size:20,
    			page_no:1,
    			total_item:2
    		}
        vm.toggleAllCheckbox=function(){
            angular.forEach(vm.auths,function(value){
                value.$selected=vm.toggleCheckbox;
            })
            if(!vm.toggleCheckbox){
              vm.showOperationButton=false;
            }
        }


        vm.tabChange=function(status){
            vm.status=status;
            initData(vm.status)
        }

        vm.showOperation=function(data){
            for(var i in vm.auths){
                if(vm.auths[i].$selected){
                    vm.showOperationButton=true;
                    return;
                }
            }
            vm.toggleCheckbox=false;
            vm.showOperationButton=false;
        }

        var initData=function(status){
            Api.Brandauth.Query({page_size:vm.pagination.page_size,page_no:vm.pagination.page_no,status:status}).$promise.then(function(data){
                if(data.code!=0)
                  vm.InfoModel('sm','加载失败','error')
                if(data.data){
                  angular.forEach(data.data.brand_authinfo_list,function(value){
                      value['$selected']=false;
                  })
                  vm.pagination.total_item=data.data?data.data.total_item:'';
                  vm.auths=data.data?data.data.brand_authinfo_list:[];
                }
            })
        }
        initData(vm.status)
        
        vm.approve=function(obj){
          var promiseList=[];
          if(obj){
            vm.EnsureApproveModel('sm',obj.partner_name,function(data){
              if(data){
                Api.Brandauth.Approve({brand_auth_id:obj.id}).$promise.then(function(data){
                  if(data.code==0)
                    vm.InfoModel('sm','操作成功','success')
                  else
                    vm.InfoModel('sm','操作失败','error')
                  initData(vm.status)
                })
              }
            })
          }
          else{
            vm.EnsureApproveModel('sm','选中项',function(data){
              if(data){
                angular.forEach(vm.auths,function(value){
                    if(value.$selected){
                      promiseList.push(Api.Brandauth.Approve({brand_auth_id:value.id}).$promise)
                    }
                })
                $q.all(promiseList).then(function(){
                  if(data.code==0)
                     vm.InfoModel('sm','操作成功','success')
                  else
                    vm.InfoModel('sm','操作失败','error')
                  initData(vm.status)
                })
              }
            })
          }
        }
        vm.decline=function(obj){
          var promiseList=[];
          if(obj){
            vm.EnsureDeclineModel('sm',obj.partner_name,function(data){
              if(data){
                Api.Brandauth.Decline({brand_auth_id:obj.id}).$promise.then(function(data){
                  if(data.code==0)
                    vm.InfoModel('sm','操作成功','success')
                  else
                    vm.InfoModel('sm','操作失败','error')
                  initData(vm.status)
                })
              }
            })
          }
          else{
            vm.EnsureDeclineModel('sm','选中项',function(data){
              if(data){
                angular.forEach(vm.auths,function(value){
                    if(value.$selected){
                      promiseList.push(Api.Brandauth.Decline({brand_auth_id:value.id}).$promise)
                    }
                })
                $q.all(promiseList).then(function(){
                  if(data.code==0)
                     vm.InfoModel('sm','操作成功','success')
                  else
                    vm.InfoModel('sm','操作失败','error')
                  initData(vm.status)
                })
              }
            })
          }
        }
        vm.EnsureDeclineModel=function openModel(size,item,callback){
              var modalInstance = $uibModal.open({
                  animation: true,
                  templateUrl: 'EnsureModel',
                  controller: 'EnsureModelCtrl',
                  size: size,
                  resolve: {
                    item: function () {
                      return '否决'+item+'的申请';
                    }
                  }
                });
                modalInstance.result.then(callback);
        }
        vm.EnsureApproveModel=function openModel(size,item,callback){
              var modalInstance = $uibModal.open({
                  animation: true,
                  templateUrl: 'EnsureModel',
                  controller: 'EnsureModelCtrl',
                  size: size,
                  resolve: {
                    item: function () {
                      return '通过'+item+'的申请';
                    }
                  }
                });
                modalInstance.result.then(callback);
        }
        vm.InfoModel = function openModel(size, info,type,callback) {
          var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'InfoModel',
            controller: 'InfoModelCtrl',
            size: size,
            resolve: {
              info: function() {
                return info;
              },
              type:function(){
                return type;
              }
            }
          });

          modalInstance.result.then(callback);
        }
        vm.reviewImgModel=function openModel(size,path,callback){
              var modalInstance = $uibModal.open({
                  animation: true,
                  templateUrl: 'ReviewImgModel',
                  controller: 'ReviewImgModelCtrl',
                  size: size,
                  resolve: {
                    path: function () {
                      return path;
                    }
                  }
                });
                modalInstance.result.then(callback);
        }
        vm.reviewImg=function(path){
            vm.reviewImgModel('lg',path,function(data){

            })
        }

    }

})();

